<?php
namespace User\Model;

use Zend\Db\TableGateway\TableGateway;

class RoleTable
{
	protected $tableGateway;
	
	/**
	 * @return the $tableGateway
	 */
	public function getTableGateway() {
		return $this->tableGateway;
	}

	/**
	 * @param TableGateway $tableGateway
	 */
	public function setTableGateway($tableGateway) {
		$this->tableGateway = $tableGateway;
	}

	public function __construct(TableGateway $tableGateway)
	{
		$this->tableGateway = $tableGateway;
	}
	
	public function fetchAll()
	{
		return $this->tableGateway->select()->toArray();
	}
	
	public function save($data)
	{
	
		$saveArr = array('key' => $data['key'], 'name' => $data['name']);
		
		if(isset($data['rid']) && $data['rid'] !== '')
		{
			//update
			$rid = (int)$data['rid'];
			unset($data['rid']);
			$where = 'rid = '.$rid;
			$this->tableGateway->update($saveArr, $where);
		}
		else
		{
			//insert
			$this->tableGateway->insert($saveArr);
		}
	}
	
	public function getById($id)
	{
		return $this->tableGateway->select(array('rid' => (int)$id));
	}
	
	public function delete($id)
	{
		$where = new \Zend\Db\Sql\Where();
		$where->equalTo('rid', intval($id));
		$this->tableGateway->delete($where);
	}
}